import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.*;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo9BulkLoading {
    // 读取DIANXIN.csv 文件 使用bulkloading加载至HBase
    public static class BulkLoaingMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            // 构建rowkey mdn_start_date
            String[] splits = value.toString().split(",");
            String mdn = splits[0];
            String start_date = splits[1];
            String country = splits[3];
            String lg = splits[4];
            String lat = splits[5];
            String rowkey = mdn + "_" + start_date;
            ImmutableBytesWritable mapKey = new ImmutableBytesWritable(rowkey.getBytes());
            KeyValue keyValue = new KeyValue(rowkey.getBytes(), "cf1".getBytes(), "country".getBytes(), country.getBytes());
            KeyValue keyValue1 = new KeyValue(rowkey.getBytes(), "cf1".getBytes(), "lg".getBytes(), lg.getBytes());
            KeyValue keyValue2 = new KeyValue(rowkey.getBytes(), "cf1".getBytes(), "lat".getBytes(), lat.getBytes());

            context.write(mapKey, keyValue);
            context.write(mapKey, keyValue1);
            context.write(mapKey, keyValue2);

        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "master,node1,node2:2181");

        Job job = Job.getInstance(conf);
        job.setJobName(Demo9BulkLoading.class.getName());
        job.setJarByClass(Demo9BulkLoading.class);

        // 配置Map任务
        job.setMapperClass(BulkLoaingMapper.class);
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setOutputValueClass(KeyValue.class);

        // 保证rk全局有序
        job.setPartitionerClass(SimpleTotalOrderPartitioner.class);

        // 设置reduce个数
        job.setNumReduceTasks(4);

        // 保证在每个reduce内有序
        job.setReducerClass(KeyValueSortReducer.class);

        // 配置输入输出路径
        FileInputFormat.addInputPath(job, new Path("/data/dianxin/"));
        FileOutputFormat.setOutputPath(job, new Path("/data/hfile/"));

        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        RegionLocator regionLocator = conn.getRegionLocator(TableName.valueOf("dianxin_bulkloading"));
        Table dianxin_bulkloading = conn.getTable(TableName.valueOf("dianxin_bulkloading"));

        // 将Reduce输出的KeyValue对象格式化成HFile格式
        HFileOutputFormat2.configureIncrementalLoad(job, dianxin_bulkloading, regionLocator);

        job.waitForCompletion(true);

        // 将生成的HFile加载到HBase中
        LoadIncrementalHFiles loadIncrementalHFiles = new LoadIncrementalHFiles(conf);
        loadIncrementalHFiles.doBulkLoad(new Path("/data/hfile/"), admin, dianxin_bulkloading, regionLocator);


        /*
            create 'dianxin_bulkloading','cf1'
            hadoop jar HBaseJavaAPI11-1.0-jar-with-dependencies.jar Demo9BulkLoading
         */
    }
}
